package org.scboy.dbms.controller;

import java.util.ArrayList;
import java.util.List;

import org.scboy.common.annotation.Menu;
import org.scboy.common.annotation.Permission;
import org.scboy.common.request.Result;
import org.scboy.dbms.service.DictionaryInfoService;
import org.scboy.utils.entitys.coobi.Model;
import org.scboy.utils.entitys.coobi.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("base/dictionaryinfo/")
@Menu("微服务管理系统.字典值管理")
public class DictionaryInfoController {
	
	@Autowired
	DictionaryInfoService dInfoService;
	
	@PostMapping("page")
	@Permission("分页查询字典值")
	public Result page(@RequestBody Model params){
		Page page=dInfoService.page(params);
		return Result.Success(page.getData(),page.getCount());	
	}
	
	@GetMapping("childs")
	@Permission("查询字典值列表")
	public Result childs(String code,String parentId){
		return Result.Success(dInfoService.childs(code,parentId),"");	
	}


	@GetMapping("tree")
	@Permission("查询字典树")
	public Result childs(String code,String parentId,String childrenName,String value,String name){
		return Result.Success(dInfoService.treeChildren(code,parentId,childrenName,value,name),"");	
	}

	
	
	@GetMapping("list")
	@Permission("查询启用的字典值列表")
	public Result list(String code,String parentId){
		return Result.Success(dInfoService.list(code,parentId),"");	
	}
	/**
	 * 新增文件字典
	 * @param request
	 * @return
	 */
	@PostMapping("add")
	@Permission("新增字典值")
	public Result add(@RequestBody Model params){
		if(dInfoService.add(params)){
			return Result.Success("BASE_DICTIONARYINFO_ADD_SUCCESS");
		}else{
			return Result.Error("BASE_DICTIONARYINFO_ADD_ERROR");
		}
	}
	
	/**
	 * 获取字典
	 * @param request
	 * @return
	 */
	@GetMapping("get")
	@Permission("获取字典值详情")
	public Result get(String id){
		return Result.Success(dInfoService.get(id),"");
	}
	/**
	 * 获取字典
	 * @param request
	 * @return
	 */
	@GetMapping("parents")
	@Permission("获取上级所有父节点")
	public Result parents(String id,String type){
		List<Model> result=new ArrayList<Model>();
		dInfoService.getParents(id, result);
		if("desc".equals(type)){
			return Result.Success(result,"");
		}else{
			List<Model> ascList=new ArrayList<Model>();
			for (int i = result.size()-1; i >=0; i--) {
				ascList.add(result.get(i));
			}
			return Result.Success(ascList,"");
		}
	}
	/**
	 * 更新字典
	 * @param request
	 * @return
	 */
	@PutMapping("update")
	@Permission("更新字典值")
	public Result update(@RequestBody Model params){
		if(dInfoService.update(params)){
			return Result.Success("BASE_DICTIONARYINFO_UPDATE_SUCCESS");
		}else{
			return Result.Error("BASE_DICTIONARYINFO_UPDATE_ERROR");
		}		
	}

	/**
	 * 删除字典
	 * @param request
	 * @return
	 */
	@DeleteMapping("delete")
	@Permission("删除字典值")
	public Result delete(String id){
		if(dInfoService.delete(id)){
			return Result.Success("BASE_DICTIONARYINFO_DELETE_SUCCESS");
		}else{
			return Result.Error("BASE_DICTIONARYINFO_DELETE_ERROR");
		}
	}
	
	/**
	 * 启用禁用字典
	 * @param request
	 * @param id
	 * @return
	 */
	@PutMapping("checkstate")
	@Permission("启用禁用字典值")
	public Result checkState(String id){
		if(dInfoService.checkState(id)){
			return Result.Success("BASE_DICTIONARYINFO_CHECKSTATE_SUCCESS");
		}else{
			return Result.Error("BASE_DICTIONARYINFO_CHECKSTATE_ERROR");
		}
	}
	

	/**
	 * 升序降序
	 * @param request
	 * @param id
	 * @return
	 */
	@PutMapping("sort")
	@Permission("升序降序")
	public Result sort(String id,int type){
		if(dInfoService.sort(id, type)){
			return Result.Success("RBAC_DICTIONARYINFO_SORT_SUCCESS");
		}else{
			return Result.Error("RBAC_DICTIONARYINFO_SORT_ERROR");
		}
	}
}
